<template>
  <div id="app">
    <div class="loading-holder" v-if="fetching"></div>
    <router-view v-else-if="isRouterAlive" :user="user" :onUpdateUser="onUpdateUser" :getUserInfo="getUserInfo"/>
  </div>
</template>
<script>
  import {isDebug} from './components/config';

  export default {
    name: 'App',
    provide() {
      return {
        reload: this.reload
      }
    },
    data() {
      return {
        user: null,
        fetching: false,
        isRouterAlive: true
      }
    },
    methods: {
      reload() {
        this.isRouterAlive = false;
        this.getUserInfo();
      },
      addLog() {
        // $.post('/emma/postdxyspend');
        console.log('src\App.vue addLog()');
        this.timer = setTimeout(() => {
          this.addLog();
        }, 5000);
      },
      getUserInfo: function () {
        $.getJSON('/emma/getdxy2019currentuser', (result) => {
          console.log('\r\n\r\n$.getJSON(\'/emma/getdxy2019currentuser\' result');
          console.log(result);
          return;

          var count = result.startcount;
          count = count < 0 ? 0 : count;
          this.user = $.extend({}, result.data, {
            starCount: count,
            times: result.todaytimes,
            cangame: result.cangame && result.todaytimes > 0
          }, {id: result.data.userid});
          this.fetching = false;
          this.isRouterAlive = true;
        })
      },
      onUpdateUser(data) {
        $.extend(this.user, data);
      }
    },
    created() {
      if (!isDebug) {
        this.addLog();
      }
      this.fetching = true;
      this.getUserInfo();
    }
  }
</script>
